<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <meta name="renderer" content="webkit|ie-comp|ie-stand">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta http-equiv="Cache-Control" content="no-siteapp"/>
        <link href="/erp/assets/css/bootstrap.min.css" rel="stylesheet"/>
        <link rel="stylesheet" href="/erp/css/style.css"/>
        <link href="/erp/assets/css/codemirror.css" rel="stylesheet">
        <link rel="stylesheet" href="/erp/assets/css/ace.min.css"/>
        <link rel="stylesheet" href="/erp/font/css/font-awesome.min.css"/>
        <!--[if lte IE 8]>
        <link rel="stylesheet" href="/erp/assets/css/ace-ie.min.css"/>
        <![endif]-->
        <script src="/erp/js/jquery-1.9.1.min.js"></script>
        <script src="/erp/assets/js/bootstrap.min.js"></script>
        <script src="/erp/assets/js/typeahead-bs2.min.js"></script>
        <script src="/erp/assets/js/jquery.dataTables.min.js"></script>
        <script src="/erp/assets/js/jquery.dataTables.bootstrap.js"></script>
        <script src="/erp/assets/layer/layer.js" type="text/javascript"></script>
        <script src="/erp/assets/laydate/laydate.js" type="text/javascript"></script>
        <script src="/erp/js/dragDivResize.js" type="text/javascript"></script>
        <title>添加角色</title></head>

    <body>
        <div class="Competence_add_style clearfix">
            <div class="left_Competence_add">
                <div class="title_name">添加角色</div>
                <div class="Competence_add">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right" for="roleName">角色序号</label>
                        <div class="col-sm-9">
                            <input type="text" id="roleNum" placeholder="" name="角色序号" class="col-xs-10 col-sm-5"></div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right" for="roleName">角色名称</label>
                        <div class="col-sm-9">
                            <span style="color: red" id="msg"></span>
                            <input type="text" id="roleName" placeholder="" name="角色名称" class="col-xs-10 col-sm-5">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right" for="roleName">角色描述</label>
                        <div class="col-sm-9">
                            <textarea id="roleDescribe" class="form-control" placeholder=""
                                      onkeyup="checkLength(this);"></textarea>
                            <span class="wordage">剩余字数：
                <span id="sy" style="color:Red;">200</span>字</span></div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right" for="roleName"></label>
                        <div class="col-sm-9"></div>
                    </div>
                    <!--按钮操作-->
                    <div class="Button_operation">
                        <a href="javascript:;">
                            <button onclick="article_save_submit();" class="btn btn-primary radius" type="submit" sf>
                                <i class="fa fa-save "></i>保存并提交
                            </button>
                        </a>
                        <a href="javascript:;">
                            <button onclick="window.history.go(-1)" class="btn btn-secondary  btn-warning"
                                    type="button">
                                <i class="fa fa-reply"></i>返回上一步
                            </button>
                        </a>
                        <button onclick="window.location.reload()" class="btn btn-default radius" type="button">&nbsp;&nbsp;重置&nbsp;&nbsp;</button>
                    </div>
                </div>
            </div>
            <!--权限分配-->
            <div class="Assign_style">
                <div class="title_name">权限分配</div>
                <div class="Select_Competence">
                    <dl class="permission-list" th:each="menu:${menus}">
                        <dt>
                            <label class="middle">
                                <input name="user-Character-0" class="ace" th:value="${menu.id}" type="checkbox"
                                       id="id-disable-check">
                                <span class="lbl" th:text="${menu.menuName}"></span></label>
                        </dt>
                        <dd th:each="menu1 : ${menu.children}">
                            <dl class="cl permission-list2">
                                <dt>
                                    <label class="middle">
                                        <input type="checkbox" th:value="${menu1.id}" class="ace"
                                               name="user-Character-0-0" id="id-disable-check">
                                        <span class="lbl" th:text="${menu1.menuName}"></span></label>
                                </dt>
                                <dd th:each="menu2:${menu1.children}" style="float: left;margin-left: 20px">
                                    <label class="middle">
                                        <input type="checkbox" th:value="${menu2.id}" class="ace"
                                               name="user-Character-0-0-0" id="user-Character-0-0-0">
                                        <span class="lbl" th:text="${menu2.menuName}"></span></label>
                                </dd>
                            </dl>
                        </dd>
                    </dl>
                </div>
            </div>
        </div>
    </body>

</html>
<script type="text/javascript">

    $("#roleNum").on("blur", function () {
        if ($(this).val().length == 0) {
            layer.tips('请输入角色序号!', '#roleNum', {
                tips: [2, 'red'],
                time: 2000
            });
        }
    })
    $("#roleName").blur(function () {
        if ($(this).val().length == 0) {
            layer.tips('请输入角色名称!', '#roleName', {
                tips: [2, 'red'],
                time: 2000
            });
        } else {
            var roleName = $("#roleName").val();
            $.post("/role/roleExist", {roleName: roleName}, function (res) {
                if (res.rs == false) {
                    layer.tips('角色名称已存在!', '#roleName', {
                        tips: [2, 'red'],
                        time: 2000
                    })
                }
            })
        }
    })

    function article_save_submit() {
        var menuIds = [];
        $.each($('input:checkbox:checked'), function () {
            //依次获取选中的菜单id值，将它存入数组中
            if ($(this).val()) {
                menuIds.push($(this).val());
            }
        });
        console.log(menuIds);
        var roleNum = $("#roleNum").val();
        var roleName = $("#roleName").val();
        var roleDescribe = $("#roleDescribe").val();
        if ($("#roleNum").val().length == 0) {
            layer.tips('请输入角色序号!', '#roleNum', {
                tips: [2, 'red'],
                time: 2000
            });
            return false;
        }
        if ($("#roleName").val().length == 0) {
            layer.tips('请输入角色名称!', '#roleName', {
                tips: [2, 'red'],
                time: 2000
            });
            return false;
        } else {
            var roleName = $("#roleName").val();
            $.post("/role/roleExist", {roleName: roleName}, function (res) {
                if (res.rs == false) {
                    layer.tips('角色名称已存在!', '#roleName', {
                        tips: [2, 'red'],
                        time: 2000
                    })
                    return false;
                } else {
                    $.post("/role/addRole?menuIds=" + menuIds, {
                            roleNum: roleNum,
                            roleName: roleName,
                            roleDescribe: roleDescribe,
                        },
                        function (res) {
                            if (res.rs) {
                                layer.msg('添加成功！', {
                                        icon: 1,
                                        time: 1000
                                    },
                                    function () {
                                        window.location.href = "/role/wt_administratorPage"
                                    });
                            } else {
                                alert("添加失败");
                                location.reload();
                            }
                        })
                }
            })
        }

    }

    //初始化宽度、高度
    $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();
    ;
    $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
    $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
    //当文档窗口发生改变时 触发
    $(window).resize(function () {

        $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
        $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
        $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();
        ;
    });

    /*字数限制*/
    function checkLength(which) {
        var maxChars = 200; //
        if (which.value.length > maxChars) {
            layer.open({
                icon: 2,
                title: '提示框',
                content: '您出入的字数超多限制!',
            });
            // 超过限制的字数了就将 文本框中的内容按规定的字数 截取
            which.value = which.value.substring(0, maxChars);
            return false;
        } else {
            var curr = maxChars - which.value.length; //250 减去 当前输入的
            document.getElementById("sy").innerHTML = curr.toString();
            return true;
        }
    };
    /*按钮选择*/
    $(function () {
        $(".permission-list dt input:checkbox").click(function () {
            $(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
        });
        $(".permission-list2 dd input:checkbox").click(function () {
            var l = $(this).parent().parent().find("input:checked").length;
            var l2 = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if ($(this).prop("checked")) {
                $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
            } else {
                if (l == 0) {
                    $(this).closest("dl").find("dt input:checkbox").prop("checked", false);
                }
                if (l2 == 0) {
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                }
            }

        });
    });</script>